#include "Game_page.h"
#include "IniProgram.h"

Container *Layout_Page = nullptr;
Container *Joystick_Control = nullptr;
Container *Joystick_Control_center = nullptr;
Container *Layout_Page_Cancle_Btn = nullptr;

void Change_Joystick()
{
    int touchX, touchY;
    if (Touch::getCurrentTouchPosition(touchX, touchY))
    {
        // 计算摇杆中心应该移动到的位置（确保摇杆完全在屏幕内）
        int joystickWidth = Joystick_Control->get_width();
        int joystickHeight = Joystick_Control->get_height();

        // 新的摇杆位置（以摇杆中心为基准）
        int newCenterX = touchX - joystickWidth / 2;
        int newCenterY = touchY - joystickHeight / 2;

        // 边界检查
        int maxX = Layout_Page->get_width() - joystickWidth;
        int maxY = Layout_Page->get_height() - joystickHeight;

        newCenterX = max(0, min(newCenterX, maxX));
        newCenterY = max(0, min(newCenterY, maxY));

        // 设置摇杆新位置
        Joystick_Control->set_pos(newCenterX, newCenterY);

        // 重置摇杆中心位置到中间
        Joystick_Control_center->align(CENTER);

        // 同时更新游戏中的摇杆位置（保持同步）
        if (Joystick != nullptr)
        {
            // 将布局页面的摇杆位置映射到游戏页面
            int gameJoystickX = newCenterX;
            int gameJoystickY = newCenterY;

            // 确保游戏摇杆也在游戏页面边界内
            int gameMaxX = Game_Page->get_width() - Joystick->get_width();
            int gameMaxY = Game_Page->get_height() - Joystick->get_height();

            gameJoystickX = max(0, min(gameJoystickX, gameMaxX));
            gameJoystickY = max(0, min(gameJoystickY, gameMaxY));

            Joystick->set_pos(gameJoystickX, gameJoystickY);
            Joystick_center->align(CENTER);
        }

        // 更新显示
        Layout_Page->updata_store();
        if (Game_Page != nullptr)
        {
            Game_Page->updata_store();
        }
    }
}

void Layout_Back()
{
    cout << "返回游戏界面" << endl;
    manager.set_top(Game_Page);
}

void Display_Layout_Page()
{
    cout << "显示布局界面" << endl;
    manager.set_top(Layout_Page);
}

void create_Layout_Page()
{
    Layout_Page = new Container(800, 480);
    Layout_Page->set_background_color(0x00ffff00);
    Layout_Page->set_Visible();
    Layout_Page->set_Event_Type(CLICK);

    Joystick_Control = new Container(Layout_Page, 200, 200);
    Joystick_Control->align(BOTTOM_LEFT, 20, -20);
    Joystick_Control->set_background_image("./res/joystick.bmp", true, 0x00ffffff);
    Joystick_Control->set_Event_Type(LONG_PRESS);
    Joystick_Control->set_Visible(true);
    Joystick_Control->set_Event(Change_Joystick);

    Joystick_Control_center = new Container(Joystick_Control, 50, 50);
    Joystick_Control_center->align(CENTER);
    Joystick_Control_center->set_Visible(true);
    Joystick_Control_center->set_background_image("./res/joystick_center.bmp", true, 0x00ffffff);

    Layout_Page_Cancle_Btn = new Container(Layout_Page, 50, 50);
    Layout_Page_Cancle_Btn->align(TOP_RIGHT, -10, 10);
    Layout_Page_Cancle_Btn->set_Visible(true);
    Layout_Page_Cancle_Btn->set_background_image("./res/cancle.bmp", true, 0x00ffffff);
    Layout_Page_Cancle_Btn->set_Event_Type(CLICK);
    Layout_Page_Cancle_Btn->set_Event(Layout_Back);

    manager.add(Layout_Page);
}